package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Hyfx;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class HyfxForm extends BasePage implements Serializable {
    private GenericManager<Hyfx, Long> hyfxManager;
    private Hyfx hyfx = new Hyfx();
    private Long id;

    public void setHyfxManager(GenericManager<Hyfx, Long> hyfxManager) {
        this.hyfxManager = hyfxManager;
    }

    public Hyfx getHyfx() {
        return hyfx;
    }

    public void setHyfx(Hyfx hyfx) {
        this.hyfx = hyfx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hyfxManager.remove(hyfx.getId());
        addMessage("hyfx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hyfx = hyfxManager.get(id);
        } else {
            hyfx = new Hyfx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hyfx.getId() == null || hyfx.getId() == 0);
        hyfxManager.save(hyfx);

        String key = (isNew) ? "hyfx.added" : "hyfx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 